//
//  FGInterestTemplateView.m
//  lite
//
//  Created by feige on 2022/11/2.
//

#import "FGInterestTemplateView.h"
#import "UIColor+FGChat.h"
#import "UIView+FGLayout.h"

@interface FGInterestTemplateView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *exchangeButton;
@property (nonatomic, strong) id templateData;
@property (nonatomic, assign) NSInteger startIndex;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation FGInterestTemplateView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        // titleLabel
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [self addSubview:_titleLabel];
        
        // tableView
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.scrollEnabled = NO;
        _tableView.rowHeight = 50;
        _tableView.separatorColor = [UIColor fg_colorWithString:@"#EBF1F5"];
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self addSubview:_tableView];
        
        // exchangeButton
        _exchangeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_exchangeButton setTitle:@"换一批" forState:0];
        [_exchangeButton setTitleColor:[UIColor fg_colorWithString:@"#558DFF"] forState:0];
        _exchangeButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        _exchangeButton.backgroundColor = [UIColor fg_colorWithString:@"#EBF2FF"];
        _exchangeButton.layer.cornerRadius = 17;
        [_exchangeButton addTarget:self action:@selector(exchangeAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_exchangeButton];
        
        _titleLabel.text = @"您可能还对以下问题感兴趣";
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _titleLabel.mm_sizeToFit().mm_right(10).mm_left(10).mm_top(10);
    
    _tableView.frame = CGRectMake(0, 42, self.mm_w, self.mm_h - 52 - (_exchangeButton.hidden ? 0 : 54));
    
    _exchangeButton.mm_left(10).mm_bottom(10).mm_width(104).mm_height(34);
}

- (void)setupDataArray
{
    NSMutableArray *arr = [NSMutableArray new];
    [((NSArray *)[self.templateData valueForKeyPath:@"data"]) enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (self.startIndex <= idx && idx < self.startIndex + 5) {
            [arr addObject:obj];
        }
    }];
    self.dataArray = arr;
    
    if (self.startIndex + 5 >= ((NSArray *)[self.templateData valueForKeyPath:@"data"]).count) {
        self.startIndex = 0;
    } else {
        self.startIndex += 5;
    }
    
    [self.tableView reloadData];
}

#pragma mark - Response Event

- (void)exchangeAction:(UIButton *)sender
{
    [self setupDataArray];
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    NSDictionary *data = self.dataArray[indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FGInterestTemplateViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FGInterestTemplateViewCell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    
    cell.textLabel.text = data[@"title"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.isPreview) {
        return;
    }
    
    NSDictionary *data = self.dataArray[indexPath.row];
    if ([self.baseDelegate respondsToSelector:@selector(templateView:didSelectedCell:)]) {
        [self.baseDelegate templateView:self didSelectedCell:data];
    }
}

#pragma mark - Rewrite Mehtod

- (void)fillData:(id)templateData {
    self.templateData = templateData;
    
    self.titleLabel.text = [self.templateData valueForKeyPath:@"tips"];
    self.exchangeButton.hidden = ((NSArray *)[self.templateData valueForKeyPath:@"data"]).count <= 5;
    
    self.startIndex = 0;
    [self setupDataArray];
}

/*
 {
     "isView":true,
     "id":1,
     "data":[
         {
             "operationType":2,
             "content":"string",
             "url":"string",
             "id":119,
             "isRecommend":true,
             "title":"string123123",
             "updateTime":"2022-11-02 10:31:14",
             "websiteId":1,
             "createTime":"2022-11-02 10:31:14",
             "companyId":1
         }
     ],
     "children":null,
     "type":1,
     "tips":"您可能还对以下问题感兴趣",
     "name":"兴趣模板",
     "pid":0
 }
 */

@end
